package com.cs.service.impl;

import com.cs.dto.ModelDto;
import com.cs.entity.Model;
import com.cs.mapper.ModelMapper;
import com.cs.req.ModelReq;
import com.cs.service.ModelService;
import com.cs.utils.oss.OssConfig;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品型号 服务实现类
 * </p>
 *
 * @author qf
 * @since 2021-08-04
 */
@Service
public class ModelServiceImpl extends ServiceImpl<ModelMapper, Model> implements ModelService {

    @Autowired
    private ModelMapper modelMapper;
    @Autowired
    private OssConfig ossConfig;

    @Override
    public Map<String, Object> search(ModelReq modelReq) {
        Map<String, Object> map = new HashMap<>();
        //获取当前页和页码长度
        Integer current = modelReq.getPageNo();
        Integer size = modelReq.getPageSize();
        //赋值
        if (current == null) {
            current = 1;
        }
        if (size == null) {
            size = 20;
        }
        //将页码和长度传入page
        Page<ModelDto> page = new Page<>(current, size);
        //创建条件对象
        QueryWrapper<Model> wrapper = new QueryWrapper<>();
        //获取品牌id和类型id
        Long brandId = modelReq.getBrandId();
        Long classifyId = modelReq.getClassifyId();
        String modelName = modelReq.getModelName();
        if (brandId != null) {
            wrapper.eq("brandId", brandId);
        }
        if (classifyId != null) {
            wrapper.eq("classifyId", classifyId);
        }
        if (modelName != null) {
            wrapper.like("modelName", modelName);
        }
        page = modelMapper.search(page, wrapper);
        List<ModelDto> records = page.getRecords();
        this.formatModelList(records);
        long pageNo = page.getCurrent();
        long pageSize = page.getSize();
        long total = page.getTotal();
        long pages = page.getPages();
        map.put("pageNo", pageNo);
        map.put("pageSize", pageSize);
        map.put("total", total);
        map.put("pages", pages);
        map.put("list", records);
        return map;
    }

    @Override
    public ModelDto getModelDetailById(Long id) {
        Model model = this.getById(id);
        ModelDto modelDto = new ModelDto(model);
        this.formatModel(modelDto);
        return modelDto;
    }

    public void formatModelList(List<ModelDto> modelDtos) {
        if (!modelDtos.isEmpty()) {
            for (ModelDto modelDto : modelDtos) {
               this.formatModel(modelDto);
            }
        }
    }

    public void formatModel(ModelDto modelDto) {
        if (!StringUtils.isBlank(modelDto.getFaceImg())) {
            modelDto.setFaceImg(ossConfig.getOssWebUrl() + modelDto.getFaceImg());
        }
        if (!StringUtils.isBlank(modelDto.getContentImg())) {
            modelDto.setContentImg(ossConfig.getOssWebUrl() + modelDto.getContentImg());
        }
    }
}
